Sample Programs > Sample Codes (Visual Basic .NET)
Sample Programs
Sample Codes (Visual Basic .NET)
The samples given in this section assumes Microsoft Visual Basic .NET for the development platform and VISA COM Software for the I/O library.
For the VISA COM Software, "KI-VISA Library (3.0.x or later)" can be downloaded from KIKUSUI Website, or NI-VISA by National Instruments (VER. 3.0 or later, VER. 3.2 or later for Windows 2000 or Windows XP) or Agilent VISA (Agilent I/O Library M.01.00 or later) by Agilent Technologies can be used.
Note that USB functions cannot be used on older versions of VISA. In addition, USB functions cannot be used on Windows 95 or Windows NT 3.5x or 4.0.
Opening a VISA session and setting communication parameters
The next code is a common section to all sample programs that are introduced later. It must be run before starting the communication with the PCR-M.
The format of the VISA resource string that is substituted in the variable strVisaAddress varies for GPIB, RS232C, and USB.
For the GPIB, device address of 5 is assumed.
For the RS232C, the following communication parameters are assumed: 19 200 bps, 8-bit data length, 1-bit stop bit, parity none, and XON/XOFF flow control. Set the PCR-M interface to match these values.
For the USB, there are no interface parameters that need to be set on the PCR-M, but the USB VID (vendor ID), PID (product ID), and serial number must be specified explicitly in the VISA resource string. The VID and PID values can be verified also on the configuration setting display of the PCR-M. The serial number can be verified on the PCR-M rear panel.
The serial number used in the sample below is an example.
Imports Ivi.Visa.Interop
Public Class Form1
Inherits System.Windows.Forms.Form
Private ioMgr As IResourceManager
Private vi As IMessage
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
' 1st try is create Global Resource Manager (It may fail if Agilent VISA)
Dim progid_GRM As String = "VISA.GlobalRM" 'GRM prog-id
ioMgr = Activator.CreateInstance(Type.GetTypeFromProgID(progid_GRM))
Catch ex As Exception
' 2nd try is create Agilent Specific Resource Manager
Dim progid_ASRM As String = "AgilentRM.SRMCls" 'Agilent SRM prog-id
ioMgr = Activator.CreateInstance(Type.GetTypeFromProgID(progid_ASRM))
End Try
' Decide VISA address
Dim strVisaAddress As String
strVisaAddress = "GPIB0::5::INSTR"
'strVisaAddress = "ASRL1::INSTR"
'strVisaAddress = "USB0::0x0B3E::0x1009::AB123456::INSTR"
' Open VISA session
vi = ioMgr.Open(strVisaAddress, AccessMode.NO_LOCK, 0, "")
Select Case vi.HardwareInterfaceType
Case 4 ' RS232
Dim serial As ISerial = vi
serial.BaudRate = 19200
serial.DataBits = 8
serial.StopBits = SerialStopBits.ASRL_STOP_ONE
serial.Parity = SerialParity.ASRL_PAR_NONE
serial.FlowControl = SerialFlowControl.ASRL_FLOW_XON_XOFF
serial.EndIn = SerialEndConst.ASRL_END_TERMCHAR
serial.EndOut = SerialEndConst.ASRL_END_TERMCHAR
vi.IOProtocol = IOProtocol.PROT_4882_STRS
vi.WriteString(":SYST:REM")
Case 7 ' USB
vi.WriteString(":SYST:REM")
End Select
vi.Timeout = 3000
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
vi.Close()
End If
MyBase.Dispose(disposing)
End Sub
From this point, communication with the PCR-M is carried out via the Visa Com IMessage interface (variable vi).
Sample code (Programming AC Output)
Dim noCulture As IFormatProvider = Globalization.CultureInfo.InvariantCulture
vi.WriteString("*RST") 'Resets the instrument
vi.WriteString(":OUTP:COUP AC") 'Selects AC coupling
vi.WriteString(":VOLT:RANG 135") 'Selects LOW (135V) range
Dim dVoltMin, dVoltMax As Double
vi.WriteString(":VOLT? MIN") 'Queries VOLT min
dVoltMin = Convert.ToDouble(vi.ReadString(256), noCulture)
vi.WriteString(":VOLT? MAX") 'Queries VOLT max
dVoltMax = Convert.ToDouble(vi.ReadString(256), noCulture)
Dim dFreqMin, dFreqMax As Double
vi.WriteString(":FREQ? MIN") 'Queries FREQ min
dFreqMin = Convert.ToDouble(vi.ReadString(256), noCulture)
vi.WriteString(":FREQ? MAX") 'Queries FREQ max
dFreqMax = Convert.ToDouble(vi.ReadString(256), noCulture)
vi.WriteString(":VOLT 110") 'Sets 110V
vi.WriteString(":FREQ 55") 'Sets 55Hz
vi.WriteString(":OUTP ON") 'Turns the output on
vi.WriteString(":SYST:ERR?") 'Queries instrument error
Dim strInstrErr As String = vi.ReadString(256)
Sample code (Programming DC Output)
Dim noCulture As IFormatProvider = Globalization.CultureInfo.InvariantCulture
vi.WriteString("*RST") 'Resets the instrument
vi.WriteString(":OUTP:COUP DC") 'Selects DC coupling
vi.WriteString(":VOLT:RANG 135") 'Selects LOW (135V) range
Dim dVoltMin, dVoltMax As Double
vi.WriteString(":VOLT:OFFS? MIN") 'Queries VOLT min
dVoltMin = Convert.ToDouble(vi.ReadString(256), noCulture)
vi.WriteString(":VOLT:OFFS? MAX") 'Queries VOLT max
dVoltMax = Convert.ToDouble(vi.ReadString(256), noCulture)
vi.WriteString(":VOLT:OFFS +40") 'Sets 40V
vi.WriteString(":OUTP ON") 'Turns the output on
vi.WriteString(":SYST:ERR?") 'Queries instrument error
Dim strInstrErr As String = vi.ReadString(256)
Sample code (Triggering Output Changes)
vi.WriteString("*RST") 'Resets the instrument
vi.WriteString(":OUTP:COUP AC") 'Selects AC coupling
vi.WriteString(":VOLT:RANG 135") 'Selects LOW (135V) range
vi.WriteString(":VOLT 110") 'Sets 110V
vi.WriteString(":FREQ 55") 'Sets 55Hz
vi.WriteString(":OUTP ON") 'Turns the output on
vi.WriteString(":VOLT:MODE STEP") 'Selects STEP mode
vi.WriteString(":FREQ:MODE STEP") 'Selects STEP mode
vi.WriteString(":VOLT:TRIG 100") 'Sets 100V when triggered
vi.WriteString(":FREQ:TRIG 50") 'Sets 50Hz when triggered
vi.WriteString(":TRIG:SEQ1:SOUR BUS") 'Selects BUS trigger
vi.WriteString(":INIT:SEQ1") 'Initiates
vi.WriteString(":TRIG:SEQ1:IMM") 'Gives a trigger
vi.WriteString(":SYST:ERR?") 'Queries instrument error
Dim strInstrErr As String = vi.ReadString(256)
Sample code (Easy Measurement)
Dim dVoltAC As Double
Dim dCurrAC As Double
Dim dCurrMax As Double
Dim dCurrMaxHold As Double
Dim dCurrCrestFactor As Double
Dim dPowACReal As Double
Dim dPowACApparent As Double
Dim dPowACReactive As Double
Dim dPowACPF As Double
Dim noCulture As IFormatProvider = Globalization.CultureInfo.InvariantCulture
' Query VOLT AC
vi.WriteString(":MEAS:VOLT:AC?")
dVoltAC = Convert.ToDouble(vi.ReadString(20), noCulture)
' Query CURR AC
vi.WriteString(":MEAS:CURR:AC?")
dCurrAC = Convert.ToDouble(vi.ReadString(20), noCulture)
' Query CURR Max
vi.WriteString(":MEAS:CURR:AMPL:MAX?")
dCurrMax = Convert.ToDouble(vi.ReadString(20), noCulture)
' Query CURR Max Hold
vi.WriteString(":MEAS:CURR:AMPL:MAX:HOLD?")
dCurrMaxHold = Convert.ToDouble(vi.ReadString(20), noCulture)
' Query CURR Crest Factor
vi.WriteString(":MEAS:CURR:CRES?")
dCurrCrestFactor = Convert.ToDouble(vi.ReadString(20), noCulture)
' Query POW AC Real
vi.WriteString(":MEAS:POW:AC:REAL?")
dPowACReal = Convert.ToDouble(vi.ReadString(20), noCulture)
' Query POW AC Apparent
vi.WriteString(":MEAS:POW:AC:APP?")
dPowACApparent = Convert.ToDouble(vi.ReadString(20), noCulture)
' Query POW AC Reactive
vi.WriteString(":MEAS:POW:AC:REAC?")
dPowACReactive = Convert.ToDouble(vi.ReadString(20), noCulture)
' Query POW AC Power Factor
vi.WriteString(":MEAS:POW:AC:PFAC?")
dPowACPF = Convert.ToDouble(vi.ReadString(20), noCulture)
Sample code (Advanced Measurement)
Dim dVoltAC As Double
Dim dCurrAC As Double
Dim dCurrMax As Double
Dim dCurrMaxHold As Double
Dim dCurrCrestFactor As Double
Dim dPowACReal As Double
Dim dPowACApparent As Double
Dim dPowACReactive As Double
Dim dPowACPF As Double
Dim noCulture As IFormatProvider = Globalization.CultureInfo.InvariantCulture
' Set up OPC event causing SRQ generation
vi.WriteString("*ESE 1;*SRE 32;*CLS")
' Initiate a new measurement
vi.WriteString(":TRIG:SEQ3:SOUR IMM")
vi.WriteString(":INIT:SEQ3")
vi.WriteString("*OPC")
' Wait for SRQ generation
Dim iStb As Short
Do
iStb = vi.ReadSTB()
Loop While (iStb And &H40) = 0
' Query VOLT AC
vi.WriteString(":FETC:VOLT:AC?")
dVoltAC = Convert.ToDouble(vi.ReadString(20), noCulture)
' Query CURR AC
vi.WriteString(":FETC:CURR:AC?")
dCurrAC = Convert.ToDouble(vi.ReadString(20), noCulture)
' Query CURR Max
vi.WriteString(":FETC:CURR:AMPL:MAX?")
dCurrMax = Convert.ToDouble(vi.ReadString(20), noCulture)
' Query CURR Max Hold
vi.WriteString(":FETC:CURR:AMPL:MAX:HOLD?")
dCurrMaxHold = Convert.ToDouble(vi.ReadString(20), noCulture)
' Query CURR Crest Factor
vi.WriteString(":FETC:CURR:CRES?")
dCurrCrestFactor = Convert.ToDouble(vi.ReadString(20), noCulture)
' Query POW AC Real
vi.WriteString(":FETC:POW:AC:REAL?")
dPowACReal = Convert.ToDouble(vi.ReadString(20), noCulture)
' Query POW AC Apparent
vi.WriteString(":FETC:POW:AC:APP?")
dPowACApparent = Convert.ToDouble(vi.ReadString(20), noCulture)
' Query POW AC Reactive
vi.WriteString(":FETC:POW:AC:REAC?")
dPowACReactive = Convert.ToDouble(vi.ReadString(20), noCulture)
' Query POW AC Power Factor
vi.WriteString(":FETC:POW:AC:PFAC?")
dPowACPF = Convert.ToDouble(vi.ReadString(20), noCulture)